﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataLayer;
using System.Windows.Threading;
using System.Windows.Media.Animation;
using System.Threading;

namespace LuanVanProject
{
    /// <summary>
    /// Interaction logic for NodeViewer.xaml
    /// </summary>
    public partial class NodeViewer : UserControl
    {
        int currentContent;
        Node node;
        public NodeViewer(Node _node)
        {
            node = _node;
            InitializeComponent();
            int currentContent = 0;
            DispatcherTimer dispatcherTimer = new DispatcherTimer();

            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            dispatcherTimer.Interval = new TimeSpan(0, 0, 5);

            dispatcherTimer.Start();
            NameLabel.Content = node.Name;
            if(node.ContentList.Count!=0)
            {
                NodeContent con = node.ContentList[currentContent];

                if (con.Type.ToLower() == "image")
                {
                    ImageContent imgCon = con as ImageContent;
                    PictureContentView img = new PictureContentView();
                    img.SetImageSource(imgCon.Content);
                    ContentPanel.Children.Add(img);
                }
                if (con.Type.ToLower() == "text")
                {
                    TextContent text = con as TextContent;
                    TextContentView tcv = new TextContentView(text.ListParagraph[0].Content);
                    ContentPanel.Children.Add(tcv);
                }
             
                currentContent++;
            }
            AmountContentNumber.Content = currentContent + "/" + node.ContentList.Count;
        }

        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            Random r = new Random();
            if (r.Next(0,7) > 3)
            {
                Thread.Sleep(r.Next(1000,4000));
                DoubleAnimation da = new DoubleAnimation();
                da.From = 1;
                da.To = 0;
                da.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
                da.AutoReverse = true;
                ContentPanel.BeginAnimation(OpacityProperty, da);
                if (node.ContentList.Count != 0 && currentContent < node.ContentList.Count)
                {
                    ContentPanel.Children.Clear();
                    AmountContentNumber.Content = currentContent + "//" + node.ContentList.Count;
                    NodeContent con = node.ContentList[currentContent];

                    if (con.Type.ToLower() == "image")
                    {
                        ImageContent imgCon = con as ImageContent;
                        PictureContentView img = new PictureContentView();
                        img.SetImageSource(imgCon.Content);
                        ContentPanel.Children.Add(img);
                    }
                    if (con.Type.ToLower() == "text")
                    {
                        TextContent text = con as TextContent;
                        TextContentView tcv = new TextContentView(text.ListParagraph[0].Content);
                        ContentPanel.Children.Add(tcv);
                    }
                    AmountContentNumber.Content = currentContent + "//" + node.ContentList.Count;
                    currentContent++;
                }
                else
                {
                    currentContent = 1;
                }
                AmountContentNumber.Content = currentContent + "/" + node.ContentList.Count;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            currentContent--;
            if (currentContent < 0)
            {
                currentContent = node.ContentList.Count();
            }
            if (node.ContentList.Count != 0 && currentContent < node.ContentList.Count)
            {
                ContentPanel.Children.Clear();
                AmountContentNumber.Content = currentContent + "//" + node.ContentList.Count;
                NodeContent con = node.ContentList[currentContent];

                if (con.Type.ToLower() == "image")
                {
                    ImageContent imgCon = con as ImageContent;
                    PictureContentView img = new PictureContentView();
                    img.SetImageSource(imgCon.Content);
                    ContentPanel.Children.Add(img);
                }
                if (con.Type.ToLower() == "text")
                {
                    TextContent text = con as TextContent;
                    TextContentView tcv = new TextContentView(text.ListParagraph[0].Content);
                    ContentPanel.Children.Add(tcv);
                }
                
                currentContent++;
            }
            else
            {
                currentContent = 1;
            }
            AmountContentNumber.Content = currentContent + "/" + node.ContentList.Count;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            currentContent++;
            if (currentContent > node.ContentList.Count)
            {
                currentContent = 1;
            }
            if (node.ContentList.Count != 0 && currentContent < node.ContentList.Count)
            {
                ContentPanel.Children.Clear();
                AmountContentNumber.Content = currentContent + "//" + node.ContentList.Count;
                NodeContent con = node.ContentList[currentContent];

                if (con.Type.ToLower() == "image")
                {
                    ImageContent imgCon = con as ImageContent;
                    PictureContentView img = new PictureContentView();
                    img.SetImageSource(imgCon.Content);
                    ContentPanel.Children.Add(img);
                }
                if (con.Type.ToLower() == "text")
                {
                    TextContent text = con as TextContent;
                    TextContentView tcv = new TextContentView(text.ListParagraph[0].Content);
                    ContentPanel.Children.Add(tcv);
                }
               
                currentContent++;
            }
            
            AmountContentNumber.Content = currentContent + "/" + node.ContentList.Count;
        }
    }
}
